package com.shi.yoyoshop.controller;

import com.shi.yoyoshop.bean.Goods;
import com.shi.yoyoshop.bean.Item;
import com.shi.yoyoshop.bean.Order;
import com.shi.yoyoshop.bean.User;
import com.shi.yoyoshop.service.GoodsService;
import com.shi.yoyoshop.service.ItemService;
import com.shi.yoyoshop.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
public class CartController {

    @Resource
    private OrderService orderService;
    @Resource
    private GoodsService goodsService;

    @Resource
    private ItemService itemService;

    /**
     * 查看购物车
     *
     * @param request
     * @return
     */
    @GetMapping("/index/cart")
    public String lookCart(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            request.setAttribute("msg", "请登录");
            return "/index/login.jsp";
        }
        return "/index/cart.jsp";
    }

    /**
     * 添加商品到购物车 | 商品 +1
     *
     * @param request
     * @param goodsId
     * @return
     */
    @ResponseBody
    @PostMapping("/index/addToCart")
    public String addToCart(HttpServletRequest request, @RequestParam("goodid") Integer goodsId) {
        // 1. 判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            return "login";
        }
        // 需要添加的商品
        Goods goods = goodsService.getGoodsById(goodsId);
        // 2. 判断库存是否充足
        if (goods.getStock() < 1) {
            return "empty";
        }

        // 3. 是否存在购物车
        Order order = (Order) request.getSession().getAttribute("order");
        if (ObjectUtils.isEmpty(order)) {
            // 新建购物车
            order = new Order();
            order.setAmount(0);
            order.setTotal(0.0);
            order.setStatus(1);
            order.setPaytype(0);
            order.setName(user.getName());
            order.setAddress(user.getAddress());
            order.setPhone(user.getPhone());
            order.setUserId(user.getId());
            order.setItemList(new ArrayList<Item>());
        }
        // 已存在购物车
        List<Item> itemList = order.getItemList();

        // 判断该商品是否在itemList中
        boolean goodsInOrder = false;
        for (Item i : itemList) {
            if (i.getGoodId().equals(goodsId)) {
                goodsInOrder = true;
                i.setAmount(i.getAmount() + 1);
                break;
            }
        }
        if (!goodsInOrder) {
            Item item = new Item();
            item.setAmount(1);
            item.setGoodId(goodsId);
            item.setGoods(goods);
            item.setPrice(goods.getPrice());
            itemList.add(item);
        }
        order.setAmount(order.getAmount() + 1);
        order.setTotal(order.getTotal() + goods.getPrice());
        request.getSession().setAttribute("order", order);
        return "ok";
    }

    /**
     * 商品 -1
     *
     * @param goodsId
     * @return
     */
    @ResponseBody
    @PostMapping("/index/lessen")
    public String decreaseGoods(HttpServletRequest request, @RequestParam("goodid") Integer goodsId) {
        // 判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            return "login";
        }
        Order order = (Order) request.getSession().getAttribute("order");
        List<Item> itemList = order.getItemList();
        for (Item item : itemList) {
            if (item.getGoodId().equals(goodsId)) {
                item.setAmount(item.getAmount() - 1);
                order.setAmount(order.getAmount() - 1);
                order.setTotal(order.getTotal() - item.getPrice());
                break;
            }
        }
        Iterator<Item> iterator = itemList.iterator();
        while (iterator.hasNext()) {
            Item item = iterator.next();
            if (item.getAmount().equals(0)) {
                iterator.remove();
            }
        }
        request.getSession().setAttribute("order", order);
        return "ok";
    }

    @ResponseBody
    @PostMapping("/index/delete")
    public String deleteGoodsFromCart(HttpServletRequest request, @RequestParam("goodid") Integer goodsId) {
        // 判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            return "login";
        }
        Order order = (Order) request.getSession().getAttribute("order");
        List<Item> itemList = order.getItemList();
        Iterator<Item> iterator = itemList.iterator();
        while (iterator.hasNext()) {
            Item item = iterator.next();
            if (item.getGoodId().equals(goodsId)) {
                order.setAmount(order.getAmount() - item.getAmount());
                order.setTotal(order.getTotal() - item.getAmount() * item.getPrice());
                iterator.remove();
            }
        }
        request.getSession().setAttribute("order", order);
        return "ok";
    }
}
